//
//  BCOrderHistoryAppealViewController.m
//  BangcaiBuy
//
//  Created by Liang Zhang on 16/7/4.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCOrderHistoryAppealViewController.h"
#import "BCOrderHistoryStuffCollectionViewCell.h"
#import "BCGaryNullCollectionViewCell.h"
#import "BCOrderHistoryAppealReasonCollectionViewCell.h"
#import "BCOrderHistoryAppealInputCollectionViewCell.h"
#import "BCOrderHistoryCommitCollectionViewCell.h"

@interface BCOrderHistoryAppealViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIWebView *phoneCallWebView;
@property (nonatomic, strong) UICollectionView *mainCollectionView;
@end

@implementation BCOrderHistoryAppealViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self p_initNavgationBar];
    [self p_initCollectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[BCShopCarManager sharedInstance] hideShopWindow];
    if (!self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    if (self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    }
}


#pragma mark - View init
-(void)p_initNavgationBar {
    self.title = @"申诉";
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)p_initCollectionView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.mainCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.view addSubview:self.mainCollectionView];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }
    else {
        return 3;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            static NSString *ID = @"HistoryStuffCollectionViewCell";
            BCOrderHistoryStuffCollectionViewCell *cell = [BCOrderHistoryStuffCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            cell.appealButton.hidden = YES;
            return cell;
        }
        else {
            static NSString *ID = @"MineGaryNullCell";
            BCGaryNullCollectionViewCell *cell = [BCGaryNullCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            return cell;
        }
    }
    else {
        if (indexPath.row == 0) {
            static NSString *ID = @"HistoryAppealReasonCollectionViewCell";
            BCOrderHistoryAppealReasonCollectionViewCell *cell = [BCOrderHistoryAppealReasonCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            return cell;
        }
        else if(indexPath.row == 1) {
            static NSString *ID = @"HistoryAppealInputCollectionViewCell";
            BCOrderHistoryAppealInputCollectionViewCell *cell = [BCOrderHistoryAppealInputCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            return cell;
        }
        else {
            static NSString *ID = @"HistoryCommitCollectionViewCell";
            BCOrderHistoryCommitCollectionViewCell *cell = [BCOrderHistoryCommitCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            cell.commit = ^() {
                //提交申诉
            };
            return cell;
        }
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    UIEdgeInsets top = {0,0,0,0};
    return top;
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return CGSizeMake(self.view.frame.size.width, 150);
        }
        else {
            return CGSizeMake(self.view.frame.size.width, 5);
        }
    }
    else {
        if (indexPath.row == 0) {
            return CGSizeMake(self.view.frame.size.width, 44);
        }
        else if (indexPath.row == 1){
            return CGSizeMake(self.view.frame.size.width, 190);
        }
        else {
            return CGSizeMake(self.view.frame.size.width, 150);
        }
    }
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && indexPath.row == 2) {
        //call
        [self CallPhone];
    }
    else if (indexPath.section == 1 && indexPath.row == 0) {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"请选择申诉原因"
                                                                       message:@""
                                                                preferredStyle:UIAlertControllerStyleActionSheet];
        
        for(int i=0;i<3;i++) {
            NSString *titleString = [NSString stringWithFormat:@"申诉原因%@",@(i)];
            UIAlertAction* reasonAction = [UIAlertAction actionWithTitle:titleString
                                                                   style:UIAlertActionStyleDestructive
                                                                 handler:^(UIAlertAction * action) {}];
            [alert addAction:reasonAction];
        }

        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定"
                                                                style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

#pragma mark - Private Action
-(void)CallPhone{
    
    NSString *phoneNum = @"4000042400";// 电话号码
    
    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]];
    
    if ( !_phoneCallWebView ) {
        
        _phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];// 这个webView只是一个后台的容易 不需要add到页面上来  效果跟方法二一样 但是这个方法是合法的
        
    }
    
    [_phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
    
}

@end
